#!/bin/bash

current_user=$(whoami)

sudo add-apt-repository multiverse -y
sudo dpkg --add-architecture i386
sudo apt update -y
sudo apt-get remove needrestart -y

echo steam steam/license note '' | sudo debconf-set-selections
echo steam steam/question select "I AGREE" | sudo debconf-set-selections
sudo apt install steamcmd -y

mkdir -p $HOME/.steam/sdk64/
steamcmd +login anonymous +app_update 1007 +quit
steamcmd +login anonymous +app_update 2394010 validate +quit

cp $HOME/Steam/steamapps/common/Steamworks\ SDK\ Redist/linux64/steamclient.so $HOME/.steam/sdk64/

cat <<EOF > pal-server-run.sh
#!/bin/bash

cd $HOME/Steam/steamapps/common/PalServer

(./PalServer.sh >> /tmp/PalServer.log &)
EOF

chmod +x pal-server-run.sh
mv pal-server-run.sh $HOME/Steam/steamapps/common/PalServer

cat <<EOF > pal-server.service
[Unit]
Description=pal-server.service

[Service]
Type=forking
User=$current_user
Restart=on-failure
RestartSec=30s
ExecStart=$HOME/Steam/steamapps/common/PalServer/pal-server-run.sh -useperfthreads -NoAsyncLoadingThread -UseMultithreadForDS

[Install]
WantedBy=multi-user.target
EOF

sudo mv pal-server.service /usr/lib/systemd/system/
sudo systemctl enable pal-server
sudo systemctl restart pal-server
